﻿  

namespace TY.SoketClient
{
    using DotNetty.Buffers;
    using DotNetty.Codecs;
    using DotNetty.Codecs.Protobuf;
    using DotNetty.Transport.Bootstrapping;
    using DotNetty.Transport.Channels;
    using DotNetty.Transport.Channels.Sockets;
    using Google.Protobuf;
    using Grpc.Core;
    using MO.Protocol;
    using System;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading.Tasks;
    using TY.SoketClient.Package.Service;

    class Program
    {
        static async Task RunClientAsync()
        {


            var group = new MultithreadEventLoopGroup();
            var STRING_ENCODER = new ProtobufEncoder();
            var STRING_DECODER = new ProtobufDecoder(BasicMessage.Parser);

     

            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                    .Group(group)
                    .Channel<TcpSocketChannel>()
                    .Option(ChannelOption.TcpNodelay, true)
                    .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
                    {
                        IChannelPipeline pipeline = channel.Pipeline;
                        pipeline.AddLast(STRING_ENCODER);
                        pipeline.AddLast(STRING_DECODER);
                        // pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter()));
                        pipeline.AddLast(new ClientHandler(new PackageManager()));
                    }));

                IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999));

                for (; ; )
                {
                    string line = Console.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    else if (line.StartsWith("zx"))
                    {
                        var PackageManager = new PackageManager();
                        while (PackageManager.Count()>0)
                        {
                            Console.WriteLine(PackageManager.PoP());
                        }
                       
                    }
                    try
                    {
                        var message = new BasicMessage();
                        message.ActionId = 1111;
                        message.Req = new BasicReq();
                        message.Req.LoginReq = new LoginReq() { DeviceId = line, Localversion = line + DateTime.Now.ToString("yyyyMMdd") };
                        await bootstrapChannel.WriteAndFlushAsync(message);


                        Console.WriteLine("消息发送完毕");

                        TreadTestClient tread = new TreadTestClient();
                        var request = new DotNettyRpc.Library.Request
                        {
                            InputNumber = 1,
                            Name = "周杰伦123"
                        };
                        tread.Foo(request, new CallOptions());
                        Console.ReadKey();

                    }
                    catch
                    {
                    }
                    if (string.Equals(line, "bye", StringComparison.OrdinalIgnoreCase))
                    {
                        await bootstrapChannel.CloseAsync();
                        break;
                    }
                }
                Console.WriteLine("结束");
                Console.ReadLine();

                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait(1000);
            }
        }

        static void Main()
        { 
            RunClientAsync().Wait();
         
        }
    }
}
